package com.grad.why.studentsystem.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.grad.why.studentsystem.domain.Camera;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.current.CurrentCaremaInfo;
import com.grad.why.studentsystem.mapper.CameraMapper;
import com.grad.why.studentsystem.service.CameraService;
import com.grad.why.studentsystem.util.Util;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;


@Component
@Service(interfaceClass = CameraService.class)
@Transactional
public class CameraServiceImpl implements CameraService {
    @Autowired
    private CameraMapper cameraMapper;

    @Override
    public List<CurrentCaremaInfo> getCameraInfos(int offset, int limit, String search) {
        try{
            return cameraMapper.getCameraInfos(offset,limit,search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getCameraInfosLength(String search) {
        try{
            return cameraMapper.getCameraInfosLength(search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int getMaxCameraId() {
        try{
            return cameraMapper.getMaxCameraId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Camera> getAllCameraInfos() {
        try{
            return cameraMapper.getAllCameraInfos();
        }catch (Exception E){
            E.printStackTrace();
        }
        return null;
    }

    @Override
    public int addCameraInfo(Camera camera) {
        try{
            return cameraMapper.insertSelective(camera);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteCameraInfoByCameraId(int i) {
        try{
            return cameraMapper.deleteByPrimaryKey(i);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateCameraInfo(Camera camera) {
        try{
            return cameraMapper.updateByPrimaryKeySelective(camera);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addCameraInfosByExcelFile(String uploadCurrentFileName) {
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            DecimalFormat decimalFormat = new DecimalFormat("##0");//格式化设置
            InputStream is = new FileInputStream(uploadCurrentFileName);
            Workbook wb = null;

            if(uploadCurrentFileName.split("\\.")[1].equals("xls")){
                wb = new HSSFWorkbook(is);
            }else if(uploadCurrentFileName.split("\\.")[1].equals("xlsx")){
                wb = new XSSFWorkbook(is);
            }

            int sheetNumber = wb.getNumberOfSheets();
            int InsertCount = 0;
            Row title = wb.getSheetAt(0).getRow(0);
            if(!title.getCell(0).getStringCellValue().equals("教室编号")||
                    !title.getCell(1).getStringCellValue().equals("教室名称")||
                    !title.getCell(2).getStringCellValue().equals("摄像头信息")){
                return 0;
            }

            for(int i = 0;i<sheetNumber;i++) {
                int index = 1;
                for(Row row : wb.getSheetAt(i)){
                    if(row == null){
                        continue;
                    }else{
                        if(index == 1){

                        }else{
                            if(row.getCell(0) == null){
                                index++;
                                continue;
                            }else{
                                Camera camera = new Camera();
                                camera.setCameraid(cameraMapper.getMaxCameraId() + 1);
                                camera.setClassroomid(Integer.parseInt(Util.formGetCellType(row.getCell(0))));
                                camera.setDeviceinfo(Util.formGetCellType(row.getCell(2)));

                                cameraMapper.insertSelective(camera);
                                InsertCount = InsertCount + 1;
                            }
                        }
                        index++;
                    }
                }
            }
            return InsertCount;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Camera> getRunningCameraInfos() {
        try{
            return cameraMapper.getRunningCameraInfos();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
